$(document).ready(function () {
    init();

    function init() {
        $.ajax({
            type: "get",
            url: urlUserCenter + "/clazz/getClazz",
            dataType: "json",
            success: function (data) {
                for (var i in data) {
                    $("#inClazz").append("<option value=" + data[i].id + ">" + data[i].name + "</option>");
                }
            },
            error: function (err) {
                console.log(err);
            }
        })
    }

    $("#questionBank").click(function () {
        $.ajax({
            type: "get",
            url: "/question/getAllQuestion",
            dataType: "json",
            success: function (data) {
                console.log(data);
                if (data.questions == null) {
                    alert("题库中没有题目!");
                } else {
                    $("#t1 tr:not(:first)").empty();//首先清空原来有的内容
                    for (var i in data.questions) {
                        var questionId = data.questions[i].id;
                        var content = data.questions[i].content;
                        $("#t1").append("<tr><td style='display: none'>" + questionId + "</td>" +
                            "<td>" + content + "</td><td>" + "<button class='choose'>选择</button>" + "</td></tr>");
                    }
                }

                $(".choose").click(function () {
                    var id = $(this).parent().parent().find("td").eq(0).text();
                    var content = $(this).parent().parent().find("td").eq(1).text();
                    $("#finalQuestion").append("<tr><td style='display: none'>" + id + "</td>" +
                        "<td>" + content + "</td><td >" + "<input type='button' class='deleteQuestion' value='移除'/>" + "</td></tr>");
                    $(".deleteQuestion").click(function () {
                        $(this).parent().parent().remove();
                    });
                })
            },
            error: function (xhr, textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });

    $("#insertTo").click(function () {
        var content = $("#tquestion").val();
        if (content == ""||content==null) {
            alert("添加内容为空");
        } else {
            $.ajax({
                type: "get",
                url: "/question/insertQuestion/" + content,
                dataType: "json",
                success: function (data) {
                    if (data.count == 0) {
                        alert("题目添加失败！");
                    } else if (data.count == 1) {
                        $.ajax({
                            type:"get",
                            url:"/question/getNewQuestion",
                            dataType:"json",
                            success:function (data) {
                                $("#finalQuestion").append("<tr><td style='display: none'>" + data.id + "</td>" +
                                    "<td>" + data.content + "</td><td >" + "<input type='button' class='deleteQuestion' value='移除'/>" + "</td></tr>");
                                alert("题目添加成功");
                            },
                            error: function (xhr, textStatus) {
                                console.log('错误');
                                console.log(xhr);
                                console.log(textStatus);
                            }
                        });
                    }
                },
                error: function (xhr, textStatus) {
                    console.log('错误');
                    console.log(xhr);
                    console.log(textStatus);
                }
            });
        }
    });

    $("#setQuestion").click(function () {
        var date = $("#tdate").val();
        var workType = $("#workType").val();
        if (date == null || date == "") {
            alert("请选择日期！");
            return;
        } else if (workType == "个人竞赛") {
            $.ajax({
                type: "get",
                url: "/setWork/checkSelfExamDate/" + date,
                dataType: "json",
                success: function (data) {
                    if (data.result == "false") {
                        alert("该日已发布结对！");
                        return;
                    } else if (data.result == "success") {
                        setSelfQuestion();
                    }
                }
            })
        } else if (workType == "结对作业") {
            $.ajax({
                type: "get",
                url: "/setWork/checkTeamExamDate/" + date,
                dataType: "json",
                success: function (data) {
                    if (data.result == "false") {
                        alert("该日已发布结对！");
                        return;
                    } else if (data.result == "success") {
                        setTeamQuestion();
                    }
                },
                error: function (xhr, textStatus) {
                    console.log('错误');
                    console.log(xhr);
                    console.log(textStatus);
                }
            });
        }
    });

    var str = "";

    function setTeamQuestion() {
        var date = $("#tdate").val();
        var classId = $("#inClazz").val();
        var tableId = document.getElementById("tques");
        var trs = tableId.rows;
        if (trs.length == 1) {
            alert("提交内容为空!!");
            return;
        }
        for (var i = 1, len = trs.length; i < len; i++) {
            var id = trs[i].cells[0].innerText;
            str += id + ",";
        }
        var teamExam = {
            questionId: str,
            classId: classId,
            date: date
        };
        $.ajax({
            type: "POST",
            url: "/setWork/addTeamExamQuestion",
            data: teamExam,
            dataType: "json",
            success: function (data) {
                $("#tques tr:not(:first)").empty();
                if (data.result == 1) {
                    alert("结对作业发布成功！");
                } else if (data.result == 0) {
                    alert("结对作业发布失败！");
                }
                console.log(data);
            },
            error: function (xhr, textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    }

    function setSelfQuestion() {
        var date = $("#tdate").val();
        var classId = $("#inClazz").val();
        var tableId = document.getElementById("tques");
        var trs = tableId.rows;
        if (trs.length == 1) {
            alert("提交内容为空!!");
            return;
        }
        for (var i = 1, len = trs.length; i < len; i++) {
            var id = trs[i].cells[0].innerText;
            str += id + ",";
        }
        var selfExam = {
            questionId: str,
            classId: classId,
            date: date
        };
        $.ajax({
            type: "POST",
            url: "/setWork/addSelfExamQuestion",
            data: selfExam,
            dataType: "json",
            success: function (data) {
                $("#tques tr:not(:first)").empty();
                if (data.result == 1) {
                    alert("竞赛作业发布成功！");
                } else if (data.result == 0) {
                    alert("竞赛作业发布失败！");
                }
                console.log(data);
            },
            error: function (xhr, textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    }

});


